<template>
  <div class="app-container">
    <div>
      <el-form ref="form" :model="form" :rules="rule" label-width="110px">
        <el-form-item label="文章标题" prop="Title">
          <el-input v-model="form.Title"></el-input>
        </el-form-item>
        <el-form-item label="文章摘要" prop="Abstract">
          <el-input v-model="form.Abstract"></el-input>
        </el-form-item>
        <el-form-item label="文章封面" prop="Cover">
          <el-upload
            class="avatar-uploader"
            action="http://api.yohao.xyz/api/upload/UpLoadFile"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.Cover" :src="form.Cover" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否开启评论" prop="Abstract">
        <el-radio-group v-model="form.IsOpenComment">
          <el-radio :label="true">开启</el-radio>
          <el-radio :label="false">关闭</el-radio>
        </el-radio-group>
        </el-form-item>
           <el-form-item label="文章标签">
            <el-tag
              v-for="tag in AllTags"
              :key="tag"
              closable
              @close="handleTagClose(tag)"
              type="info"
            >
              {{ tag }}
            </el-tag>
            <el-input
              class="input-new-tag"
              v-if="tagVisible"
              v-model="inputTag"
              ref="saveTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm"
              @blur="handleInputConfirm"
            >
            </el-input>
            <el-button
              v-else
              class="button-new-tag"
              size="small"
              @click="showInput"
              >+ 新增</el-button
            >
          </el-form-item>
        <el-form-item label="文章正文" prop="Content">
            <tinymce :height="300" v-model="form.Content" />
        </el-form-item>
      </el-form>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
    <span  v-if="form.ID==0">
      <el-button type="primary" @click="handleSubmit(1)">确定新增</el-button>
      <el-button type="primary" @click="handleSubmit(2)">保存草稿</el-button>
    </span>
      <el-button v-else type="success" @click="handleSubmit(form.Status)">确定修改</el-button>
    </span>
  </div>
</template>

<script>
import req from "@/utils/req";
import Pagination from "@/components/Pagination";
import Tinymce from "@/components/Tinymce";
export default {
  components: { Pagination, Tinymce },
  data() {
    return {
      SearchForm: {
        KeyWord: "",
        pageIndex: 1,
        pageSize: 20
      },
      form: {
        ID: 0,
        Abstract: "",
        Cover: "",
        Content: "",
        IsOpenComment:true,
        Tag: "",
        Status:1,
      },
      AllTags: [],
      TotalCount: 0,
      DataList: [],
      RoleList: [],
      AddOrEditdialogVisible: false, //
      listLoading: false,
      tagVisible: false,
      inputTag: "",
      //验证
      rule: {
        Title: [{ required: true, message: "请输入文章标题", trigger: "blur" }],
        Abstract: [{ required: true, message: "请输入文章摘要", trigger: "blur" }],
        Cover: [{ required: true, message: "请添加封面图", trigger: "blur" }],
        Content: [{ required: true, message: "请输入文章正文", trigger: "blur" }],
      }
    };
  },
  watch: {},
  created() {

     this.form.ID = parseInt( this.$route.params.id);
     if(this.form.ID !=0){
     this.$req.get('Blog/Article/Model',{ID:this.form.ID}).then(res=>{
       this.form=res.Data;
       this.AllTags = this.form.Tag.split(",");
     });;
     }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.form.Cover = res.Data;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 4;
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 4MB!");
      }
      return isLt2M;
    },
    closeSelectedTag(){
        let view=this.$route;
         this.$store.dispatch('delView', view).then(({ visitedViews }) => {
        if (true) {
          const latestView = visitedViews.slice(-1)[0]
          if (latestView) {
            this.$router.push(latestView)
          } else {
            this.$router.push('/')
          }
        }
      })
    },
    //新增
    handleAdd() {
      this.AddOrEditdialogVisible = true;
    },
    handleUpdate(row) {
      this.form = Object.assign({}, row); // copy obj;
      this.AllTags = this.form.Tags.split(",");
      this.AddOrEditdialogVisible = true;
    },
    //关闭的回调
    handleClose() {
      // console.log(this.$refs)
      this.closeSelectedTag();
    // this.$refs.tagView.closeSelectedTag(this.$refs.tagView.selectedTag)
    },
    handleSubmit(status) {
      this.form.Status=status;
      var url = this.form.ID == 0 ? "Blog/Article/Add" : "Blog/Article/Edit";
      this.form.Tags = this.AllTags.toString();
      this.$refs["form"].validate(valid => {
        if (valid) {
          req.post(url, this.form).then(res => {
            if (res.StatusCode === 200) {
              this.$notify.success({ title: "成功", message: "操作成功" });
              this.closeSelectedTag();
            } else {
              this.$notify.error({ title: "失败", message: res.Data });
            }
          });
        }
      });
    },
    showInput() {
      this.tagVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let inputValue = this.inputTag;
      if (inputValue) {
        if (this.AllTags.indexOf(inputValue) > -1) {
          this.$message({ type: "error", message: "该标签已存在" });
          return false;
        }
        this.AllTags.push(inputValue);
      }
      this.tagVisible = false;
      this.inputTag = "";
    },
    handleTagClose(tag) {
      this.AllTags.splice(this.AllTags.indexOf(tag), 1);
    }
  },
  watch: {
    AllTags: {
      handler: function(newval, oldval) {
        if (newval) {
          this.form.Tag = newval.toString();
        }
      },
      deep: true
    }
  }
};
</script>
<style >
.el-form{
  width: 80%;
}

.avatar-uploader div {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
